/**
 * validateForm.js
 * v1.0
 * 2/20/2011
 * 
 * The Javascript code 
 */

//Validates if there is no error on the form
function validateAnalysisRequest(){
	var current;
	var i;				
	var flag = 0;
	
	for(i = 1; i < 5; i++){
		switch (i){
			case 1: current = "ClientName"; break;
			case 2: current = "ClientCompany"; break;
			case 3: current = "ClientAddress"; break;
			case 4: current = "ClientTelfax"; break;
		}
		
		if(isBlank(document.getElementById(current).value)){
			printError(current,0); 
			flag = 1;
		}else if(!verifyField(document.getElementById(current).value,i)){
			printError(current,1);
			flag = 1;
		}else printError(current,-1);
	}
	
	if(flag == 0) return true;
		
	return false;
}

//Prints the error beside the textfield
function printError(field,x){
	var str="";
	switch(x){
		case 0: str = "This Field is required."; break;
		case 1: str = "Invalid Character/s" ; break; 
		default: str = ""; break;
	}
	
	document.getElementById("error"+field).innerHTML = str;
		
}

//Checks if the textfield is null
function isBlank(fieldValue){	
	if(fieldValue == "" || fieldValue == null){				
		return true;
	}
	return false;
}

//Checks if there are invalid characters
function verifyField(fieldValue,x){
	var pattern;
	
	switch(x){		
		case 0: pattern = /\w/g; break;				//Pattern for PreparedBy
		case 1: pattern = /\w/g; break;				//Pattern for ClientName
		case 2: pattern = /[\w\d]/g; break;			//Pattern for ClientCompany
		case 3: pattern = /[\w\d]/g; break;			//Pattern for ClientAddress
		case 4: pattern = /[\d]/g; break;			//Pattern for ClientTelfax
		default: alert("An error occured! Try again.");
	}
	
	if(pattern.test(fieldValue)){
		return true;
	}else return false;
	
	return false;
}